﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Register.Command
{
    public class FactoryHelper<T> where T : class
    {
        private static T instance = null;

        /// <summary>
        /// 通过反射技术，得到类对象
        /// </summary>
        /// <param name="typeNameKey">类型的key</param>
        /// <param name="nameSpace">命名空间</param>
        /// <param name="assemblyPath">类型路径</param>
        /// <returns></returns>
        public static T Create(string typeNameKey, string nameSpace, string assemblyPath)
        {
            if (instance == null)
            {
                string typename = ConfigurationManager.AppSettings[typeNameKey];
                string classname = nameSpace.Trim() + "." + typename.Trim();

                instance = (T)Assembly.Load(assemblyPath.Trim()).CreateInstance(classname);
            }

            return instance;

        }
    }
}
